package com.capacitydorm.server.web;

import com.capacitydorm.server.common.util.JWTUtil;
import com.capacitydorm.server.common.vo.LoginVo;
import com.capacitydorm.server.common.vo.Result;
import com.capacitydorm.server.domain.DataUser;
import com.capacitydorm.server.domain.User;
import com.capacitydorm.server.service.LoginService;
import com.capacitydorm.server.service.WxuserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private WxuserService wxuserService;

    @Resource
    private LoginService loginService;
    /**
     * 微信登陆
     * @param user
     * @return
     */
    @PostMapping("/wxlogin")
    public ResponseEntity<Result> WXLogin(User user){
        try {
            if(user.getUsername()==null || user.getPassword() == null){
                return new ResponseEntity<>(new Result(200,"用户名或密码不能为空",new LoginVo(false,null)), HttpStatus.OK);
            }
            List<User> login = wxuserService.login(user);
            if (login.size() == 0){
                return new ResponseEntity<>(new Result(200,"用户名或密码错误",new LoginVo(false,null)), HttpStatus.OK);
            }else if (login.size()==1){
                String sign = JWTUtil.sign(login.get(0).getUsername(), "vince");
                String username =  JWTUtil.getUsername(sign);
                return new ResponseEntity<>(new Result(200,"登陆成功",new LoginVo(true,login.get(0).getWxid(),sign)), HttpStatus.OK);
            }else {
                return new ResponseEntity<>(new Result(200,"系统出错",new LoginVo(false,null)), HttpStatus.OK);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    @PostMapping("/login")
    public ResponseEntity<Result> Login(DataUser user){
        try {
            if(user.getUsername()==null || user.getPassword() == null){
                return new ResponseEntity<>(new Result(200,"用户名或密码不能为空",new LoginVo(false,null)), HttpStatus.OK);
            }
            List<DataUser> login = loginService.login(user);
            if (login.size() == 0){
                return new ResponseEntity<>(new Result(200,"用户名或密码错误",new LoginVo(false,null)), HttpStatus.OK);
            }else if (login.size()==1){
                String sign = JWTUtil.sign(login.get(0).getUsername(), "vince");
                String username =  JWTUtil.getUsername(sign);
                return new ResponseEntity<>(new Result(200,"登陆成功",new LoginVo(true,login.get(0).getId(),sign)), HttpStatus.OK);
            }else {
                return new ResponseEntity<>(new Result(200,"系统出错",new LoginVo(false,null)), HttpStatus.OK);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
